import sys
parent_dir = "../pygl"
sys.path.append(parent_dir)
import OpenGL.GL as gl
import glm
import imgui
import numpy as np
from common.app_v1 import App
import pygl

class LineApp(App):
    # 初始化着色器，vao vbo等
    def setupVertex(self) -> None:
        self.program = pygl.ProgramVF("basic/shaders/line.vs", 
                                      "basic/shaders/line.fs")

        line1_position = np.array([
            -0.8, 0.0, 0.0,
            0.8, 0.0, 0.0
        ], dtype=gl.GLfloat)
        self.line1_position_vbo = pygl.VertexBufferObject(line1_position)
        
        line1_color = np.array([ 
            1.0, 0.0, 0.0,
            1.0, 0.0, 0.0
        ], dtype=gl.GLfloat)        
        self.line1_color_vbo = pygl.VertexBufferObject(line1_color)

        attribute_position = pygl.VertexAttribute("position", 0, 3, 
                                                  gl.GL_FLOAT, False, 0)
        attribute_color = pygl.VertexAttribute("color", 1, 3, 
                                                  gl.GL_FLOAT, False, 0)

        self.vao1 = pygl.VertexArrayObject()
        line1_position_binding_point = 4
        self.vao1.setVertexBuffer(self.line1_position_vbo, 
                                  line1_position_binding_point, 
                                  0, 3 * gl.sizeof(gl.GLfloat))
        self.vao1.setVertexAttribute(line1_position_binding_point, 
                                     attribute_position)
        
        line1_color_binding_point = 6
        self.vao1.setVertexBuffer(self.line1_color_vbo, 
                                  line1_color_binding_point, 
                                  0, 3 * gl.sizeof(gl.GLfloat))
        self.vao1.setVertexAttribute(line1_color_binding_point, 
                                     attribute_color)

        line2_vertices = np.array([
            # 位置          颜色
            0.0, -0.8, 0.0, 0.0, 1.0, 0.0,
            0.0, 0.8, 0.0,  0.0, 1.0, 0.0
        ], dtype=gl.GLfloat)
        self.line2_vbo = pygl.VertexBufferObject(line2_vertices)

        line2_attribute_color = pygl.VertexAttribute("color", 1, 3, 
                                                  gl.GL_FLOAT, False, 
                                                  3 * gl.sizeof(gl.GLfloat))
        self.vao2 = pygl.VertexArrayObject()       
        line2_binding_point = 9
        self.vao2.setVertexBuffer(self.line2_vbo, line2_binding_point, 
                                  0, 6 * gl.sizeof(gl.GLfloat))

        self.vao2.setVertexAttribute(line2_binding_point, attribute_position)
        self.vao2.setVertexAttribute(line2_binding_point, line2_attribute_color)

        self.line_width = 2.0

    # 配置imgui界面元素
    def showImGui(self)->None:
        imgui.begin("设置")
        _, new_width = imgui.slider_float("线宽", self.line_width, 1.0, 10.0)
        self.line_width = new_width
        _, color = imgui.color_edit3("背景颜色", self.background_color.x, 
                                     self.background_color.y, 
                                     self.background_color.z)
        self.background_color = glm.vec4(color[0], color[1], color[2], 1.0)
        imgui.end()       

    # 渲染图形
    def render(self) -> None:
        gl.glClearNamedFramebufferfv(0, gl.GL_COLOR, 0, self.background_color.to_tuple())
        self.program.use()
        gl.glLineWidth(self.line_width)

        self.vao1.bind()
        gl.glDrawArrays(gl.GL_LINES, 0, 2)

        self.vao2.bind()
        gl.glDrawArrays(gl.GL_LINES, 0, 2)

    # 释放资源
    def cleanup(self) -> None:
        self.line1_position_vbo.delete()
        self.line1_color_vbo.delete()
        self.line2_vbo.delete()
        self.vao1.delete()
        self.vao2.delete()
        self.program.delete()

if __name__ == "__main__":
    app = LineApp(100, 100, 800, 600, "你好，线段App V1.0！")
    app.run()

